<?php if($products): ?>
<div id="product-partial-table">
<h3><?php echo $partial_payment;  ?></h3>
<table class="list">
    <thead>
    <tr>
        <td class="left"></td>
        <td class="left"><?php echo $column_product; ?></td>
        <td class="left"><?php echo $column_model; ?></td>
        <td class="right"><?php echo $column_quantity; ?></td>
        <td class="right"><?php echo $column_price; ?></td>
        <td class="right"><?php echo $column_total; ?></td>
        <td class="right"><?php echo $column_payment; ?></td>
        <td class="right"><?php echo $column_paid; ?></td>
        <td class="right"><?php echo $column_remaining_pay; ?></td>
        <td class="right"><?php echo $column_status; ?></td>
    </tr>
    </thead>
    <tbody>
    <?php foreach ($products as $product): ?>
    <tr id="hidden-<?php echo $product['order_product_partial_id']; ?>">
        <input type="hidden" name="partial[<?php echo $product['order_product_partial_id']; ?>][product_real_id]" value="<?php echo $product['product_real_id']; ?>" />
        <input type="hidden" name="partial[<?php echo $product['order_product_partial_id']; ?>][price]" value="<?php echo $product['price']; ?>" />
        <input type="hidden" name="partial[<?php echo $product['order_product_partial_id']; ?>][name]" value="<?php echo $product['name']; ?>" />
        <input type="hidden" name="partial[<?php echo $product['order_product_partial_id']; ?>][product_id]" value="<?php echo $product['product_id']; ?>" />
        <input type="hidden" name="partial[<?php echo $product['order_product_partial_id']; ?>][sent]" value="<?php echo $product['sent']; ?>" />
        <input id="money-left-hidden-<?php echo $product['order_product_partial_id']; ?>" type="hidden" name="partial[<?php echo $product['order_product_partial_id']; ?>][money_left]" value="<?php echo $product['money_left']; ?>" />
        <input type="hidden" name="partial[<?php echo $product['order_product_partial_id']; ?>][order_id]" value="<?php echo $product['order_id']; ?>" />
    </tr>

    <tr id="partial-product-<?php echo $product['partial_id']; ?>">
        <td class="left"><img onclick="remove_from_partial(<?php echo $product['partial_id']; ?>);" style="cursor: pointer;" alt="Удалить" title="Удалить" src="view/image/delete.png"></td>
        <td class="left"><?php echo $product['name']; ?></td>
        <td class="left"><?php echo $product['model']; ?></td>
        <td class="right"><input class="quantity" id="quantity-<?php echo $product['order_product_partial_id']; ?>" type="text" name="partial[<?php echo $product['order_product_partial_id'] ?>][quantity]" value="<?php echo $product['quantity']; ?>" /></td>
        <td class="right price"><?php echo $product['price']; ?></td>
        <td id="total-<?php echo $product['order_product_partial_id']; ?>" class="right"><?php echo $product['total']; ?></td>
        <td class="right">
            <select name="partial[<?php echo $product['order_product_partial_id']; ?>][payment]">
                <?php foreach($product['payment'] as $code=>$method): ?>
                <option<?php if($code==$product['productPayment']): ?> selected="true"<?php endif; ?> value="<?php echo $code; ?>"><?php echo $method; ?></option>
                <?php endforeach; ?>
            </select>
        </td>
        <td class="right"><input class="money" id="money-<?php echo $product['order_product_partial_id']; ?>" type="text" name="partial[<?php echo $product['order_product_partial_id']; ?>][money]" value="<?php echo $product['money']; ?>" /></td>
        <td id="money-left-<?php echo $product['order_product_partial_id']; ?>" class="right"><?php echo $product['money_left']; ?></td>
        <td class="right">
            <select name="partial[<?php echo $product['order_product_partial_id'] ?>][status]">
                <?php foreach($product['status'] as $num=>$status): ?>
                <option<?php if($num==$product['productStatus']): ?> selected="true"<?php endif; ?> value="<?php echo $num; ?>"><?php echo $status; ?></option>
                <?php endforeach; ?>
            </select>
        </td>
    </tr>
    <?php endforeach; ?>
    <tr>
        <td class="center" colspan="10"><?php //echo $text_no_results; ?></td>
    </tr>
    </tbody>
</table>
</div>

<script type="text/javascript">
    $('input.quantity').live('change',function(){
        if($(this).val()>0){
            var id=$(this).attr('id').replace('quantity-','');
            var total=$('#total-'+id);
            var moneyLeft=$('#money-left-'+id);
            var moneyLeftHidden=$('#money-left-hidden-'+id);
            var newTotal=$(this).val()*$('td.price').html();
            var newLeft=newTotal-$('#money-'+id).val();


            total.html(newTotal.toFixed(4));
            moneyLeft.html(newLeft.toFixed(4));
            moneyLeftHidden.val(newLeft.toFixed(4));
        }else{
            $(this).val(1);
            alert("<?php echo $error_quantity; ?>");
        }

    });

    $('input.money').live('change',function(){
        var id=$(this).attr('id').replace('money-','');
        var money=$(this).val();
        var total=$('#total-'+id).html();

        if(money>=0 && money<=total){
            var moneyLeft=$('#money-left-'+id);
            var moneyLeftHidden=$('#money-left-hidden-'+id);
            var left=total-money;

            if(left>=0){
                moneyLeft.html(left.toFixed(4));
                moneyLeftHidden.val(left.toFixed(4));
            }
        }else{
            $(this).val(0);
            alert("<?php echo $error_money; ?>");
        }
    });

    function remove_from_partial(partialId){
        if(partialId){
            var url=window.location.search;
            $.ajax({
                url: url.replace('order/update','partial_payment/delete_from_partial'),
                type: 'get',
                data: 'partial_id='+partialId,
                dataType: 'json',
                success: function(json){
                    if(json.error){
                        $('.warning').removeAttr('style');
                        $('.warning').html(json.error);
                    }
                    if(json.success){
                        $('.success').removeAttr('style');
                        $('.success').html(json.success);
                    }
                    if(json.result){
                        $('#partial-product-'+partialId).remove();
                        $('#hidden-'+partialId).remove();
                    }
                }
            });
        }
    }
</script>
<?php endif; ?>